﻿using System;
using ERP_RORZE_SYSTEM;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ERP_RORZE_DLL.Business
{
    public partial class panel_PartMistakeStatus : UserControl
    {
        private string detail_orderno;
        private string detail_poid;
        private int stat;

        public panel_PartMistakeStatus()
        {
            InitializeComponent();
            this.stat = 0;
            this.buttonRefresh.Visible = true;
            this.buttonDetailsMistake.Visible = true;
            this.buttonDetailsReturn.Visible = true;
            this.buttonAddMistake.Visible = true;
            this.buttonAddReturn.Visible = true;

            this.mistakeRefresh.Visible = false;
            this.mistakeAdd.Visible = false;
            this.mistakeEdit.Visible = false;
            this.buttonSummary.Visible = false;

            this.gridPartMistakeStatus.BringToFront();
            PartMistakeStatus_LoadData();
            //PartMistake_LoadData();
            this.PartMistakeStatus.DoubleClick += PartMistakeStatus_DoubleClick;
            this.PartMistakeStatus.PopupMenuShowing += PartMistakeStatus_PopupMenuShowing;
            this.partMistakeStatusContext.ItemClicked += partMistakeStatusContext_ItemClicked;
            
            this.PartMistake.DoubleClick += PartMistake_DoubleClick;
            this.PartMistake.PopupMenuShowing += PartMistake_PopupMenuShowing;
            this.partMistakeContext.ItemClicked += partMistakeContext_ItemClicked;

            this.PartMistakeReturn.DoubleClick += PartMistakeReturn_DoubleClick;
            this.PartMistakeReturn.PopupMenuShowing += PartMistakeReturn_PopupMenuShowing;
            this.partMistakeReturnContext.ItemClicked += partMistakeReturnContext_ItemClicked;
        }

        ////////////////////////////////////////////
        /// Call Mistake Edit Form
        ////////////////////////////////////////////
        private void callMistakeEditForm()
        {
            if (PartMistake.FocusedRowHandle >= 0)
            {
                DataRow row = PartMistake.GetFocusedDataRow();
                frm_Edit_PartMistake edit_form = new frm_Edit_PartMistake();
                edit_form.poMistakeID = row["ID"].ToString();
                edit_form.LoadData();
                edit_form.ShowDialog();
                PartMistake_LoadData();
            }
            else
            {
                MessageBox.Show("Please select record to edit");
            }
        }

        ////////////////////////////////////////////
        /// Call Mistake Return Edit Form
        ////////////////////////////////////////////
        private void callMistakeReturnEditForm()
        {
            if (PartMistakeReturn.FocusedRowHandle >= 0)
            {
                DataRow row = PartMistakeReturn.GetFocusedDataRow();
                frm_Edit_PartMistakeReturn edit_return_form = new frm_Edit_PartMistakeReturn();
                edit_return_form.poid = row["POID"].ToString();
                edit_return_form.orderno = row["OrderNo"].ToString();
                edit_return_form.mistakereturnid = Int32.Parse(row["ID"].ToString());
                edit_return_form.LoadData();
                edit_return_form.ShowDialog();
                PartMistakeReturn_LoadData();
            }
            else
            {
                MessageBox.Show("Please select record to edit");
            }
        }

        /////////////////////////////////////////////
        /// Selection From Context Menu on Return
        /////////////////////////////////////////////
        
        void partMistakeReturnContext_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.partMistakeReturnContext.Hide();            
            switch (e.ClickedItem.Name)
            {
                case "returnEditSelection":
                    callMistakeReturnEditForm();
                    break;
                case "returnAddNewSelection":
                    frm_Add_PartMistakeReturn add_return_form = new frm_Add_PartMistakeReturn();
                    add_return_form.ShowDialog();
                    PartMistakeReturn_LoadData();
                    break;
                case "returnSummarySelection":
                    callSummaryPanel();                    
                    break;                
                case "returnRefreshSelection":
                    PartMistakeReturn_LoadData();
                    break;
            }
        }
        
        /////////////////////////////////////////////
        /// Show Context Menu on Return
        /////////////////////////////////////////////
        void PartMistakeReturn_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            this.partMistakeReturnContext.Show(MousePosition);
        }

        ////////////////////////////////////////////
        /// Part Return Double Click to edit
        ////////////////////////////////////////////
        void PartMistakeReturn_DoubleClick(object sender, EventArgs e)
        {
            callMistakeReturnEditForm();
        }

        ////////////////////////////////////////////
        /// Details Double Click to edit
        ////////////////////////////////////////////        
        void PartMistake_DoubleClick(object sender, EventArgs e)
        {
            callMistakeEditForm();
        }

        /////////////////////////////////////////////
        /// Selection From Context Menu on Summary
        /////////////////////////////////////////////
        void partMistakeStatusContext_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.partMistakeStatusContext.Hide();            
            switch (e.ClickedItem.Name)
            {
                case "detailsSelection":
                    callMistakePanel();                    
                    break;
                case "returnSelection":
                    callReturnPanel();                    
                    break;
                case "newPartMistakeSelection":
                    frm_Add_PartMistake add_form = new frm_Add_PartMistake();
                    add_form.ShowDialog();
                    PartMistakeStatus_LoadData();
                    break;
                case "newPartReturnSelection":
                    frm_Add_PartMistakeReturn add_return_form = new frm_Add_PartMistakeReturn();
                    add_return_form.ShowDialog();
                    PartMistakeStatus_LoadData();
                    break;
                case "summaryRefreshSelection":
                    PartMistake_LoadData();
                    break;
            }
        }

        void PartMistakeStatus_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            partMistakeStatusContext.Show(MousePosition);
        }

        void PartMistake_PopupMenuShowing(object sender, DevExpress.XtraGrid.Views.Grid.PopupMenuShowingEventArgs e)
        {
            partMistakeContext.Show(MousePosition);
        }

        /////////////////////////////////////////////
        /// Selection From Context Menu on Details
        /////////////////////////////////////////////
        void partMistakeContext_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            this.partMistakeContext.Hide();            
            switch (e.ClickedItem.Name)
            {
                case "editSelection":
                    callMistakeEditForm();                    
                    break;
                case "addSelection":
                    frm_Add_PartMistake add_form = new frm_Add_PartMistake();
                    add_form.ShowDialog();
                    PartMistake_LoadData();
                    break;
                case "summarySelection":
                    callSummaryPanel();                    
                    break;
                case "detailsRefreshSelection":
                    PartMistake_LoadData();
                    break;
            }
        }


        ////////////////////////////////////////
        /// Summary double click to show details
        ////////////////////////////////////////
        void PartMistakeStatus_DoubleClick(object sender, EventArgs e)
        {
            callMistakePanel();            
        }


        //////////////////////////////////////////////////
        /// Load Data for Part Mistake Summary
        //////////////////////////////////////////////////
        private void PartMistakeStatus_LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            INISettings mySettings = new INISettings();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySettings.SQLConnectionString;
            string sql = "SELECT [241_POMistake].POID, [241_POMistake].OrderNo, [241_POMistake].PartNo,"
                + " [222_Supplier].SupplierName, SUM([241_POMistake].Quantity) AS SumNG,"
                + " CASE WHEN SUM([241_POMistakeReturn].Quantity) IS NULL THEN 0 ELSE SUM([241_POMistakeReturn].Quantity) END AS ReturnNG,"
                + " CASE WHEN SUM([241_POMistakeReturn].Quantity) IS NULL THEN SUM([241_POMistake].Quantity)"
                + " ELSE SUM([241_POMistake].Quantity) - SUM([241_POMistakeReturn].Quantity) END AS Remain"
                + " FROM [241_POMistake]"
                + " LEFT JOIN [241_POMistakeReturn] ON [241_POMistake].POID = [241_POMistakeReturn].POID"
                + " AND [241_POMistake].OrderNo = [241_POMistakeReturn].OrderNo"
                + " LEFT JOIN [222_Supplier] ON [241_POMistake].SupplierID = [222_Supplier].SupplierID"
                + " GROUP BY [241_POMistake].POID, [241_POMistake].OrderNo, [222_Supplier].SupplierName,"
                + " [241_POMistake].PartNo";
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            if (sourceDataSet != null)
            {
                gridPartMistakeStatus.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("POID", typeof(string));
                newTable.Columns.Add("OrderNo", typeof(string));
                newTable.Columns.Add("PartNo", typeof(string));
                newTable.Columns.Add("Supplier", typeof(string));
                newTable.Columns.Add("SumOfNG", typeof(int));
                newTable.Columns.Add("SumOfReturn", typeof(int));
                newTable.Columns.Add("NGRemain", typeof(int));                                
                gridPartMistakeStatus.DataSource = newTable;
            }
            PartMistakeStatus.Columns[0].Caption = "PO ID";
            PartMistakeStatus.Columns[1].Caption = "Order No";
            PartMistakeStatus.Columns[2].Caption = "PartNo";
            PartMistakeStatus.Columns[3].Caption = "Supplier";
            PartMistakeStatus.Columns[4].Caption = "Sum of NG";
            PartMistakeStatus.Columns[5].Caption = "Sum of Return";
            PartMistakeStatus.Columns[6].Caption = "NG Remain";                        
            PartMistakeStatus.BestFitColumns();
        }

        ///////////////////////////////////////////////////
        /// Load data for Part Mistake Details
        ///////////////////////////////////////////////////
        private void PartMistake_LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            INISettings mySettings = new INISettings();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySettings.SQLConnectionString;
            string sql;
            if ((string.IsNullOrEmpty(detail_poid)) || (string.IsNullOrEmpty(detail_orderno)))
            {
                sql = "SELECT t1.[ID], t1.[InputDate], t1.[InforNo], t1.[POID], t1.[OrderNo], t1.[PartNo],"
                    + " t2.[SupplierName], t1.[Quantity], t1.[SPQuantity], t1.[MistakeContent], t1.[Solution]"
                    + " FROM [241_POMistake] AS t1 LEFT JOIN"
                    + " [222_Supplier] AS t2 ON t1.SupplierID = t2.SupplierID";
            }
            else
            {
                sql = "SELECT t1.[ID], t1.[InputDate], t1.[InforNo], t1.[POID], t1.[OrderNo], t1.[PartNo],"
                    + " t2.[SupplierName], t1.[Quantity], t1.[SPQuantity], t1.[MistakeContent], t1.[Solution]"
                    + " FROM [241_POMistake] AS t1 LEFT JOIN"
                    + " [222_Supplier] AS t2 ON t1.SupplierID = t2.SupplierID"
                    + " WHERE t1.POID='" + detail_poid + "' AND t1.OrderNo='" + detail_orderno + "'";
            }
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            //sourceDataSet = mySQLManagement.SQL_Select("[241_POMistake]", "");
            if (sourceDataSet != null)
            {
                gridPartMistake.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("ID", typeof(int));
                newTable.Columns.Add("InpuDate", typeof(DateTime));
                newTable.Columns.Add("InfoNo", typeof(string));
                newTable.Columns.Add("POID", typeof(string));
                newTable.Columns.Add("OrderID", typeof(string));
                newTable.Columns.Add("PartNo", typeof(string));
                newTable.Columns.Add("Supplier", typeof(string));
                newTable.Columns.Add("Quantity", typeof(int));
                newTable.Columns.Add("SPQuantity", typeof(int));
                newTable.Columns.Add("MistakeContent", typeof(string));
                newTable.Columns.Add("Solution", typeof(string));
                gridPartMistake.DataSource = newTable;
            }
            PartMistake.Columns[0].Caption = "ID";
            PartMistake.Columns[1].Caption = "Input Date";
            PartMistake.Columns[2].Caption = "Info No";
            PartMistake.Columns[3].Caption = "PO ID";
            PartMistake.Columns[4].Caption = "Order No";
            PartMistake.Columns[5].Caption = "Part No";
            PartMistake.Columns[6].Caption = "Supplier";
            PartMistake.Columns[7].Caption = "Quantity";
            PartMistake.Columns[8].Caption = "SP Quantity";
            PartMistake.Columns[9].Caption = "Mistake Content";
            PartMistake.Columns[10].Caption = "Solution";
            PartMistake.Columns[0].Visible = false;
            PartMistake.BestFitColumns();
        }

        ///////////////////////////////////////////////////
        /// Load data for Part Mistake Return
        ///////////////////////////////////////////////////
        private void PartMistakeReturn_LoadData()
        {
            DataSet sourceDataSet = new DataSet();
            INISettings mySettings = new INISettings();
            SQLManagement mySQLManagement = new SQLManagement();
            mySQLManagement.SQLConnectionString = mySettings.SQLConnectionString;
            string sql;
            if ((string.IsNullOrEmpty(detail_poid)) || (string.IsNullOrEmpty(detail_orderno)))
            {
                sql = "SELECT * FROM [241_POMistakeReturn]";
            }
            else
            {
                sql = "SELECT * FROM [241_POMistakeReturn] WHERE POID='" + detail_poid + "' AND OrderNo='" + detail_orderno + "'";
            }
            sourceDataSet = mySQLManagement.SQL_Query(sql);
            //sourceDataSet = mySQLManagement.SQL_Select("[241_POMistake]", "");
            if (sourceDataSet != null)
            {
                gridPartMistakeReturn.DataSource = sourceDataSet.Tables[0];
            }
            else
            {
                DataTable newTable = new DataTable();
                newTable.Columns.Add("ID", typeof(int));
                newTable.Columns.Add("InpuDate", typeof(DateTime));
                newTable.Columns.Add("POID", typeof(string));
                newTable.Columns.Add("OrderID", typeof(string));
                newTable.Columns.Add("Quantity", typeof(int));
                gridPartMistakeReturn.DataSource = newTable;
            }
            PartMistakeReturn.Columns[0].Caption = "ID";
            PartMistakeReturn.Columns[1].Caption = "Input Date";
            PartMistakeReturn.Columns[2].Caption = "PO ID";
            PartMistakeReturn.Columns[3].Caption = "Order No";
            PartMistakeReturn.Columns[4].Caption = "Quantity";
            PartMistakeReturn.Columns[0].Visible = false;
            PartMistakeReturn.BestFitColumns();
        }

        ///////////////////////////////////////////
        /// Summary - Details Switch
        ///////////////////////////////////////////
        private void buttonSwitch_Click(object sender, EventArgs e)
        {
            callMistakePanel();          
        }

        //////////////////////////////////////////
        /// Call Summary Panel
        //////////////////////////////////////////
        private void callSummaryPanel()
        {
            this.stat = 0;
            this.buttonRefresh.Visible = true;
            this.buttonDetailsMistake.Visible = true;
            this.buttonDetailsReturn.Visible = true;
            this.buttonAddMistake.Visible = true;
            this.buttonAddReturn.Visible = true;

            this.mistakeRefresh.Visible = false;
            this.mistakeAdd.Visible = false;
            this.mistakeEdit.Visible = false;
            this.buttonSummary.Visible = false;
            this.labelControl1.Text = "Part Mistake -Summary";
            this.gridPartMistakeStatus.BringToFront();
            PartMistakeStatus_LoadData();
        }

        //////////////////////////////////////////
        /// Call Mistake Panel
        //////////////////////////////////////////
        private void callMistakePanel()
        {
            DataRow row = PartMistakeStatus.GetFocusedDataRow();
            this.stat = 1;

            this.buttonRefresh.Visible = false;
            this.buttonDetailsMistake.Visible = false;
            this.buttonDetailsReturn.Visible = false;
            this.buttonAddMistake.Visible = false;
            this.buttonAddReturn.Visible = false;

            this.mistakeRefresh.Visible = true;
            this.mistakeAdd.Visible = true;
            this.mistakeEdit.Visible = true;
            this.buttonSummary.Visible = true;

            this.labelControl1.Text = "Part Mistake - Details";
            detail_orderno = row["OrderNo"].ToString();
            detail_poid = row["POID"].ToString();
            PartMistake_LoadData();
            this.gridPartMistake.BringToFront();
        }

        //////////////////////////////////////////
        /// Call Mistake Return Panel
        //////////////////////////////////////////
        private void callReturnPanel()
        {
            DataRow row = PartMistakeStatus.GetFocusedDataRow();
            this.stat = 2;
            
            this.buttonRefresh.Visible = false;
            this.buttonDetailsMistake.Visible = false;
            this.buttonDetailsReturn.Visible = false;
            this.buttonAddMistake.Visible = false;
            this.buttonAddReturn.Visible = false;

            this.mistakeRefresh.Visible = true;
            this.mistakeAdd.Visible = true;
            this.mistakeEdit.Visible = true;
            this.buttonSummary.Visible = true;

            this.labelControl1.Text = "Part Mistake Return - Details";
            detail_orderno = row["OrderNo"].ToString();
            detail_poid = row["POID"].ToString();
            PartMistakeReturn_LoadData();
            this.gridPartMistakeReturn.BringToFront();
        }


        private void buttonDetailsReturn_Click(object sender, EventArgs e)
        {
            callReturnPanel();
        }

        private void buttonAddReturn_Click(object sender, EventArgs e)
        {
            frm_Add_PartMistakeReturn add_return_form = new frm_Add_PartMistakeReturn();
            add_return_form.ShowDialog();
            PartMistakeStatus_LoadData();
        }

        private void buttonAddMistake_Click(object sender, EventArgs e)
        {
            frm_Add_PartMistake add_form = new frm_Add_PartMistake();
            add_form.ShowDialog();
            PartMistakeStatus_LoadData();
        }

        private void buttonRefresh_Click(object sender, EventArgs e)
        {
            PartMistakeStatus_LoadData();
        }

        private void mistakeRefresh_Click(object sender, EventArgs e)
        {
            switch (stat)
            {
                case 1:
                    PartMistake_LoadData();
                    break;
                case 2:
                    PartMistakeReturn_LoadData();
                    break;
            }
        }

        private void mistakeAdd_Click(object sender, EventArgs e)
        {
            switch (stat)
            {
                case 1:
                    frm_Add_PartMistake add_form = new frm_Add_PartMistake();
                    add_form.ShowDialog();
                    PartMistake_LoadData();
                    break;
                case 2:
                    frm_Add_PartMistakeReturn add_return_form = new frm_Add_PartMistakeReturn();
                    add_return_form.ShowDialog();
                    PartMistakeReturn_LoadData();
                    break;
            }
        }

        private void mistakeEdit_Click(object sender, EventArgs e)
        {
            switch (stat)
            {
                case 1:
                    callMistakeEditForm();
                    break;
                case 2:
                    callMistakeReturnEditForm();
                    break;
            }
        }

        private void buttonSummary_Click(object sender, EventArgs e)
        {
            callSummaryPanel();
        }
    }
}
